package com.fyzk.activity.special;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.lottery.togetherbuy.HM_Popupwindow;
import com.fyzk.activity.user.NetProgressView;
import com.fyzk.activity.user.RecordBaseActivity;
import com.fyzk.bean.BetRecordResponse;
import com.fyzk.interfaces.MoreInterface;
import com.fyzk.interfaces.ReTryInterface;
import com.fyzk.interfaces.UpdateInterface;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.MsgListView;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by fanxs11 on 17/2/14.
 */

@ContentView(R.layout.dj_usercenter_bet)
public class VirtualBetActivity extends RecordBaseActivity implements onTopTitleClickListener,
        MoreInterface, ReTryInterface, UpdateInterface {

    @ViewInject(R.id.user1_bet_topTitle)
    private TopTitleView topView;

    private String[] lottery_popup_items = {"全部彩种", "竞彩足球", "竞彩篮球", "足彩", "双色球", "大乐透", "山东11选5",
            "广东11选5",
            "排列三", "排列五", "福彩3D", "七星彩", "七乐彩"};
    private int[] lottery_popup_itemIDs = {-1, LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT,
            LotteryIDUtils.JLSF_LOTTERY_ID_INT, LotteryIDUtils.ZCSFC_LOTTERY_ID_INT,
            LotteryIDUtils.SSQ_LOTTERY_ID_INT, LotteryIDUtils.DLT_LOTTERY_ID_INT, LotteryIDUtils
            .SD11XUAN5_LOTTERY_ID_INT, LotteryIDUtils.GD11XUAN5_LOTTERY_ID_INT,
            LotteryIDUtils.PL3_LOTTERY_ID_INT, LotteryIDUtils.PL5_LOTTERY_ID_INT, LotteryIDUtils
            .FC3D_LOTTERY_ID_INT, LotteryIDUtils.QXC_LOTTERY_ID_INT,
            LotteryIDUtils.QLC_LOTTERY_ID_INT};
    private HM_Popupwindow lottery_popupView;
    private String[] status_popup_items = {"全部", "未开奖", "未中奖", "中奖", "撤单"};
    private int[] status_popup_itemIDs = {-1, 3, 1, 2, 4};
    private HM_Popupwindow status_popupView;
    private String[] time_popup_items = {"全部", "最近一周", "最近一月", "最近三月"};
    private int[] time_popup_itemIDs = {1, 2, 3, 4};
    private HM_Popupwindow time_popupView;
//    @ViewInject(R.id.user1_bet_lottery_tv)
//    private TextView lottery_tv;
//    @ViewInject(R.id.user1_bet_status_tv)
//    private TextView status_tv;
//    @ViewInject(R.id.user1_bet_time_tv)
    private TextView time_tv;
    @ViewInject(R.id.recordlist)
    MsgListView listViewExt;

    @ViewInject(R.id.recordProgressView)
    NetProgressView recordProgressView;
    VirtualHallAdapter listAdapter;
    public List<BetRecordResponse.OrdersBean> infos;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        initView();
        requestBetRecord();
    }

    private void requestBetRecord() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("state", "" + status_popup_itemIDs[status_popupView.getCheckItem()])
            ;    // 查询类型 (-1不限 1未中奖 2中奖 3未开奖 4撤单)
            request_json.put("pageIndex", search);
            request_json.put("maxResult", 20);
            request_json.put("lotteryType", lottery_popup_itemIDs[lottery_popupView.getCheckItem
                    ()]);    // 彩种类型 (竞彩足球3006 竞彩篮球3001 北单普通5001 足彩4001)
            request_json.put("timeRange",3);
            // 时间范围 (1全部 2一周 3一个月 4三个月)
            httpPostJson(CommandUtil.FINDVIRTUALHISTORYBET, Commands.FINDVIRTUALHISTORYBET, request_json,
                    true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private BetRecordResponse betRecordResponse;

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        recordProgressView.dismiss();
        betRecordResponse = JSON.parseObject(resultBean.getData(), BetRecordResponse.class);

        if (search == 1) {
            this.infos = betRecordResponse.getOrders();

            if (infos.size() < 20) {
                listViewExt.listMore.setClickable(false);
                listViewExt.listMore.setTextStr("无更多数据");
            } else {
                listViewExt.listMore.endNet();
                listViewExt.listMore.setTextStr("获取更多");
            }
            listAdapter.setInfors(this.infos);
        } else {
            this.infos.addAll(betRecordResponse.getOrders());

            if (betRecordResponse.getOrders().size() < 20) {
                listViewExt.listMore.setClickable(false);
                listViewExt.listMore.setTextStr("无更多数据");
            } else {
                listViewExt.listMore.endNet();
            }
            listAdapter.setInfors(this.infos);
        }
        if (isUpdate) {
            listViewExt.onRefreshComplete();
        }
    }

    @Override
    protected void onFailure(int requestCode, String errorcode,String message) {
        if(errorcode.equals("900003")){
            recordProgressView.noneRecord(message);
        }
        listAdapter.setInfors(new ArrayList<BetRecordResponse.OrdersBean>());
        if (isUpdate) {
            listViewExt.onRefreshComplete();
        }
        listAdapter.notifyDataSetChanged();
    }

    private void initView() {
        topView.setRightVisible(View.INVISIBLE);
        topView.setCenterText(R.string.usercenter_bet);
        topView.setOnTitleClickListener(this);
        topView.setCenterText("虚拟投注列表");
        lottery_popupView = new HM_Popupwindow(this, AbsListView.LayoutParams.MATCH_PARENT,
                AbsListView.LayoutParams.WRAP_CONTENT,
                lottery_popup_items, false);
        lottery_popupView.setHm_PopupOnItemListener(new HM_Popupwindow.HM_PopuponItemListener() {
            @Override
            public void onItem(int position) {
             //   lottery_tv.setText(lottery_popup_items[position]);
                requestBetRecord();
            }

            @Override
            public void popupShow() {
              //  lottery_tv.setBackgroundResource(R.drawable.xxk_bg_red);
               // lottery_tv.setTextColor(getResources().getColor(R.color.textcolor_red));
            }

            @Override
            public void popupDismiss() {
              //  lottery_tv.setBackgroundResource(R.drawable.xxk_bg_gray);
             //   lottery_tv.setTextColor(getResources().getColor(R.color.textcolor_black));
            }
        });

        status_popupView = new HM_Popupwindow(this, AbsListView.LayoutParams.MATCH_PARENT,
                AbsListView.LayoutParams.WRAP_CONTENT,
                status_popup_items, false);
        status_popupView.setHm_PopupOnItemListener(new HM_Popupwindow.HM_PopuponItemListener() {
            @Override
            public void onItem(int position) {
                //status_tv.setText(status_popup_items[position]);
                requestBetRecord();
            }

            @Override
            public void popupShow() {
                //status_tv.setBackgroundResource(R.drawable.xxk_bg_red);
               // status_tv.setTextColor(getResources().getColor(R.color.textcolor_red));
            }

            @Override
            public void popupDismiss() {
             //   status_tv.setBackgroundResource(R.drawable.xxk_bg_gray);
             //   status_tv.setTextColor(getResources().getColor(R.color.textcolor_black));
            }
        });

        time_popupView = new HM_Popupwindow(this, AbsListView.LayoutParams.MATCH_PARENT,
                AbsListView.LayoutParams.WRAP_CONTENT,
                time_popup_items, false);
        time_popupView.setHm_PopupOnItemListener(new HM_Popupwindow.HM_PopuponItemListener() {
            @Override
            public void onItem(int position) {
                time_tv.setText(time_popup_items[position]);
                requestBetRecord();
            }

            @Override
            public void popupShow() {
                time_tv.setBackgroundResource(R.drawable.xxk_bg_red);
                time_tv.setTextColor(getResources().getColor(R.color.textcolor_red));
            }

            @Override
            public void popupDismiss() {
                time_tv.setBackgroundResource(R.drawable.xxk_bg_gray);
                time_tv.setTextColor(getResources().getColor(R.color.textcolor_black));
            }
        });

      //  lottery_tv.setOnClickListener(this);
      //  status_tv.setOnClickListener(this);
        time_tv.setOnClickListener(this);

        listAdapter = new VirtualHallAdapter(getContext(), infos);
        listViewExt.setAdapter(listAdapter);
        listViewExt.setUpdateInterface(this);
        listViewExt.setMoreInterface(this);

    }

    @Override
    public void onLeftClick() {
        // TODO Auto-generated method stub
        onBackPressed();
    }

    @Override
    public void onCenterClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRightClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRight2Click() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View arg0) {
//        switch (arg0.getId()) {
//            case R.id.user1_bet_lottery_tv:
//                lottery_popupView.showPopupWindow(lottery_tv);
//                status_popupView.dismiss();
//                time_popupView.dismiss();
//                break;
//            case R.id.user1_bet_status_tv:
//                status_popupView.showPopupWindow(status_tv);
//                lottery_popupView.dismiss();
//                time_popupView.dismiss();
//                break;
//            case R.id.user1_bet_time_tv:
//                time_popupView.showPopupWindow(time_tv);
//                lottery_popupView.dismiss();
//                status_popupView.dismiss();
//                break;
//            default:
//                break;
//        }
    }

    @Override
    public void more() {
        // TODO Auto-generated method stub
        search++;
        requestBetRecord();
    }

    @Override
    public void reTry() {
        requestBetRecord();
    }

    @Override
    public void update() {
        // TODO Auto-generated method stub
        isUpdate = true;
        search = 1;
        requestBetRecord();
    }
}
